package iot.jtt808.terminal;

import iot.jtt808.Decoder;
import iot.jtt808.Encoder;
import iot.utils.DataPackUtil;

/**
 * Created by dave on 18-7-20 下午2:39.
 * 终端注册，0x0100
 */
public class TerminalRegister implements Decoder<TerminalRegister>, Encoder {
    /**
     * 省域 ID（参考  GB/T 2260 中规定的行政区划代码六位中前两位）
     */
    private int provinceId;

    /**
     * 市县域 ID（参考  GB/T 2260 中规定的行政区划代码六位中后四位）
     */
    private int cityId;

    /**
     * 制造商 ID
     */
    private String makerId;

    /**
     * 终端型号
     */
    private String model;

    /**
     * 终端 ID
     */
    private String deviceId;

    /**
     * 车牌颜色（参考 JT/T415-2006 的 5.4.12）<br>
     *     0-未上牌, 1-蓝色, 2-黄色, 3-黑色, 4-白色
     */
    private int colorId;

    private String vin;

    private String license;

    @Override
    public TerminalRegister decode(byte[] bytes, int start, int end) {
        provinceId = DataPackUtil.readWord(bytes, start);
        cityId = DataPackUtil.readWord(bytes, start + 2);
        makerId = DataPackUtil.bytesToString(bytes, start + 4, start + 9);
        model = DataPackUtil.bytesToString(bytes, start + 9, start + 17);
        deviceId = DataPackUtil.bytesToString(bytes, start + 17, start + 24);
        colorId = bytes[start + 24];
        if (colorId == 0) {
            vin = DataPackUtil.bytesToString(bytes, start + 25, end);
        } else {
            license = DataPackUtil.bytesToString(bytes, start + 25, end);
        }
        return this;
    }

    @Override
    public byte[] encode() {
        byte[] vl;
        if (colorId == 0) {
            vl = DataPackUtil.stringToByteArray(vin);
        } else {
            vl = DataPackUtil.stringToByteArray(license);
        }

        byte[] ret = new byte[25 + vl.length];
        DataPackUtil.writeWord(ret, 0, provinceId);
        DataPackUtil.writeWord(ret, 2, cityId);
        System.arraycopy(makerId.getBytes(), 0, ret, 4, 5);
        System.arraycopy(model.getBytes(), 0, ret, 9, 8);
        System.arraycopy(deviceId.getBytes(), 0, ret, 17, 7);
        DataPackUtil.writeByte(ret, 24, colorId);
        System.arraycopy(vl, 0, ret, 25, vl.length);
        return ret;
    }

    public int getProvinceId() {
        return provinceId;
    }

    public void setProvinceId(int provinceId) {
        this.provinceId = provinceId;
    }

    public int getCityId() {
        return cityId;
    }

    public void setCityId(int cityId) {
        this.cityId = cityId;
    }

    public String getMakerId() {
        return makerId;
    }

    public void setMakerId(String makerId) {
        this.makerId = makerId;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public int getColorId() {
        return colorId;
    }

    public void setColorId(int colorId) {
        this.colorId = colorId;
    }

    public String getVin() {
        return vin;
    }

    public void setVin(String vin) {
        this.vin = vin;
    }

    public String getLicense() {
        return license;
    }

    public void setLicense(String license) {
        this.license = license;
    }
}
